package cn.formaggie.controller.manage;

import cn.formaggie.annotation.CheckTwiceSubmit;
import cn.formaggie.controller.BaseController;
import cn.formaggie.entity.model.PageBean;
import cn.formaggie.entity.model.roleandpermission.Role;
import cn.formaggie.service.face.RolePermissionService;
import cn.formaggie.service.face.RoleService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiresAuthentication
@Controller
@RequestMapping("/back/role")
public class RoleManageController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @RequiresPermissions({"职位管理"})
    @RequestMapping("/list")
    public ModelAndView rolesList(@ModelAttribute(STATE_MSG)String stateMsg){
        ModelAndView mav=new ModelAndView();
        mav.addObject(STATE_MSG,stateMsg);
        mav.addObject("mainPage",BACK_PAGE_PATH+"/manager/role/list.jsp");
        mav.setViewName("/backpage/backTemp");
        return mav;
    }

    @RequiresPermissions({"职位管理"})
    @RequestMapping("/load")
    @ResponseBody
    public Map load(@RequestParam(value = "s_roleName",required = false) String s_roleName,
                    @RequestParam(value = "page",required = false) Integer page,
                    @RequestParam(value = "limit",required = false) Integer limit){

        PageBean pageBean=new PageBean(page,limit);
        Map<String,Object> sqlMap=new HashMap<>();
        sqlMap.put("roleName",s_roleName);
        sqlMap.put("pageBean",pageBean);
        List<Role> roles=roleService.findRoles(sqlMap);
        Integer count=roleService.getRolesCount(sqlMap);

        Map<String,Object> viewMap=new HashMap<>();
        viewMap.put("data",roles);
        viewMap.put("count",count);
        viewMap.put("code",0);
        viewMap.put("msg","");
        return viewMap;
    }


    @RequiresPermissions({"职位管理"})
    @RequestMapping("/add")
    @CheckTwiceSubmit
    public String addRole(Role role, RedirectAttributes model) {
        Boolean state=roleService.addRole(role);
        model.addFlashAttribute(STATE_MSG,state?"添加成功":"添加失败");
        return "redirect:/back/role/list";
    }

    @RequiresPermissions({"职位管理"})
    @RequestMapping("/modifyInfo")
    @CheckTwiceSubmit
    public String modifyRoleInfo(Role role,RedirectAttributes model){
        if(role.getId()==1){
            model.addFlashAttribute(STATE_MSG,"修改失败");
            return "redirect:/back/role/list";
        }
        Boolean state=roleService.modifyRoleInfo(role);
        model.addFlashAttribute(STATE_MSG,state?"修改成功":"修改失败");
        return "redirect:/back/role/list";
    }


    @RequiresPermissions({"职位管理"})
    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteRole(Integer id){
        if(id==1){
            return false;
        }
        return roleService.deleteRole(id);
    }
}
